package com.ostraffel.recallvocabularybackend.service.serviceutils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ostraffel.recallvocabularybackend.DTO.OriginWord;
import com.ostraffel.recallvocabularybackend.DTO.UserConfigDetailed;
import com.ostraffel.recallvocabularybackend.DTO.Word;
import com.ostraffel.recallvocabularybackend.mapper.users.sqluserinterface.SqlUserInterface;
import com.ostraffel.recallvocabularybackend.service.sqlutilesinterface.SqlUtilsInterface;

@Component
public class Utils {
    @Autowired
    public SqlUtilsInterface sqlUtilsInterface;

    public void TransportWordsMapper() {
        List<OriginWord> originWords = sqlUtilsInterface.GetWordsFromOrigin();

        List<Word> words = MappingWords(originWords);

        for (Word word : words) {
            sqlUtilsInterface.AddWordsToTable(word.getId(), word.getSpell(), word.getIpa(), word.getExplication(), word.getProficiency(), word.getLastDate(), word.getUserId());
        }
    }

    public void GnerateUserTable(String userId,String dictName) {
        try {
            List<Word> words = sqlUtilsInterface.GetWordsFromPrototypeTable();
            for (Word word : words) {
                System.out.println(word.toString());
                sqlUtilsInterface.CopyWordsToProductTable(word.getId(), word.getSpell(), word.getIpa(), word.getExplication(), word.getProficiency(), word.getLastDate(), userId);
            }
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private List<Word> MappingWords(List<OriginWord> originWords) {
        List<Word> ret = new ArrayList<>();
        Date today = new Date();
        int i = 0;
        for(OriginWord originWord : originWords) {
            Word word = new Word();
            word.setId(String.valueOf(i));
            word.setSpell(originWord.getWord());
            word.setIpa("NA");
            word.setExplication(originWord.getTranslate());
            word.setProficiency(0);
            word.setLastDate(today);
            word.setUserId("default");
            ret.add(word);
            i++;
        }
        return ret;
    }

    @Autowired
    private SqlUserInterface sqlUserInterface;
    public void AddUserMapper(UserConfigDetailed userConfigDetailed) {
        sqlUserInterface.addUser(userConfigDetailed.getUserId(), userConfigDetailed.getUserName(), userConfigDetailed.getUserPassword(), userConfigDetailed.getUserEmail(), userConfigDetailed.getNewWordNumber(), userConfigDetailed.getRecallWordNumber());
    }
}
